package com.swfgui.utils.display
{
	import flash.display.DisplayObjectContainer;
	import flash.display.FrameLabel;
	import flash.display.MovieClip;

	/**
	 * 控制播放，连同子元件
	 * @author jasonliu
	 */
	public class MovieClipUtil
	{
		public function MovieClipUtil()
		{
		}
				
		public static function stopAllMovieClip(container:DisplayObjectContainer):void
		{
			if(container is MovieClip)
			{
				MovieClip(container).stop();
			}
			
			var mcArr:Array = DisplayUtil.searchChild(container, MovieClip);
			for each(var child:MovieClip in mcArr)
			{
				child.stop();
			}
		}		
		
		public static function playAllMovieClip(container:DisplayObjectContainer):void
		{
			var mcArr:Array = DisplayUtil.searchChild(container, MovieClip);
			for each(var child:MovieClip in mcArr)
			{
				child.play();
			}
		}		
		
		public static function stop(mc:MovieClip):void
		{
			mc.stop();
			var mcArr:Array = DisplayUtil.searchChild(mc, MovieClip);
			for each(var child:MovieClip in mcArr)
			{
				child.stop();
			}
		}
		
		public static function play(mc:MovieClip):void
		{
			mc.play();
			var mcArr:Array = DisplayUtil.searchChild(mc, MovieClip);
			for each(var child:MovieClip in mcArr)
			{
				child.play();
			}
		}
		
		public static function prevFrame(mc:MovieClip):void
		{
			mc.prevFrame();
			var mcArr:Array = DisplayUtil.searchChild(mc, MovieClip);
			for each(var child:MovieClip in mcArr)
			{
				child.stop();
				child.prevFrame();
			}
		}
		
		public static function nextFrame(mc:MovieClip):void
		{
			mc.nextFrame();
			var mcArr:Array = DisplayUtil.searchChild(mc, MovieClip);
			for each(var child:MovieClip in mcArr)
			{
				child.stop();
				child.nextFrame();
			}
		}
		
		public static function gotoAndStop(mc:MovieClip, frame:Object):void
		{
			var targetFrame:int = frame is String ? getFrameByLabel(mc, String(frame)) : int(frame);
			if(targetFrame <= 0 || targetFrame > mc.totalFrames)
			{
				return;
			}
			
			if(targetFrame > mc.currentFrame)
			{
				//todo 此次居然会卡死，也就是说mc.nexFrame()以后，mc.currentFrame不变
				while(mc.currentFrame != targetFrame)
				{
					nextFrame(mc);
				}
			}
			else if(targetFrame < mc.currentFrame)
			{
				while(mc.currentFrame != targetFrame)
				{
					prevFrame(mc);
				}
			}
			
			stop(mc);
		}
		
		public static function gotoAndPlay(mc:MovieClip, frame:Object):void
		{
			var targetFrame:int = frame is String ? getFrameByLabel(mc, String(frame)) : int(frame);
			if(targetFrame <= 0 || targetFrame > mc.totalFrames)
			{
				return;
			}
			
			if(targetFrame > mc.currentFrame)
			{
				while(mc.currentFrame != targetFrame)
				{
					nextFrame(mc);
				}
			}
			else if(targetFrame < mc.currentFrame)
			{
				while(mc.currentFrame != targetFrame)
				{
					prevFrame(mc);
				}
			}
			
			play(mc);
		}
			
		public static function getFrameByLabel(mc:MovieClip, label:String):int
		{
			for each(var fb:FrameLabel in mc.currentLabels)
			{
				if(fb.name == label)
				{
					return fb.frame;
				}
			}
			
			return -1;
		}
	}
}